package griffin.website;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

//import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
//import android.view.Gravity;
//import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
//import android.view.MotionEvent;
import android.view.View;
//import android.view.View.OnClickListener;
//import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
//import android.widget.Button;
//import android.widget.EditText;
//import android.widget.ImageView;
//import android.widget.LinearLayout;
//import android.widget.LinearLayout.LayoutParams;
//import android.widget.PopupWindow;
import android.widget.RadioGroup;
//import android.widget.Toast;
import griffin.logic.userLogic;
//import android.view.KeyEvent;
//import android.app.AlertDialog;
//import android.content.DialogInterface;
//import android.app.ActivityManager;
//import android.content.Context;
import android.content.res.Configuration;

public class TabBarFramework extends ScrollableTabActivity {
	public final static String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/sitedata";
	public final static String DATABASE_FILENAME = "web.sqlite";	//database name
	userLogic userlogic = new userLogic(this);
	private static final String TAG = "[TabBarFramework]";
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG,"[onCreate...]" );

		Init();
	}	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// TODO Auto-generated method stub		
//		Log.d(TAG,"[onConfigurationChanged...]" );
		Init();
	}
	@Override
	public void onResume() {
		super.onResume();
//		Log.d(TAG,"[onResume...]" );
//		Init();
		
	}
	private void Init(){
		initDatabase();
		/*
		 * set this activity as the tab bar delegate
		 * so that onTabChanged is called when users tap on the bar
		 */
		setDelegate(new SliderBarActivityDelegateImpl());
		
		//clear
		intentList  = new ArrayList();
		titleList	= new ArrayList();
		states 		= new ArrayList();
		
		this.addTab("HOME", R.drawable.home, RadioStateDrawable.SHADE_GRAY,RadioStateDrawable.SHADE_GREEN,new Intent(this, TopHome.class));
		this.addTab("SEARCH", R.drawable.search, RadioStateDrawable.SHADE_GRAY,RadioStateDrawable.SHADE_GREEN, new Intent(this, SearchSite.class));
		this.addTab("BOOKMARK", R.drawable.star, RadioStateDrawable.SHADE_GRAY, RadioStateDrawable.SHADE_GREEN,new Intent(this, bookmark.class));
		this.addTab("MORE", R.drawable.more, RadioStateDrawable.SHADE_GRAY,RadioStateDrawable.SHADE_GREEN,new Intent(this, More.class));
		/*
		 * commit is required to redraw the bar after add tabs are added
		 * if you know of a better way, drop me your suggestion please.
		 */
		commit();
		
        //set windows
		Intent intent=getIntent(); 
        String value=intent.getStringExtra("winname"); 
        Log.d(TAG,"[VALUE ...]"+value );
        if(value==null){
        	isFirst=0;
        	setCurrentTab(0);
        	isFirst=1;
        }
        else if(value.equals("list")){
        	openNewAcitvityWithView("List",list.class);
        }else if(value.equals("contentlist")) {
        	openNewAcitvityWithView("ContentList",contentlist.class);
        }else if(value.equals("more")){
        	openNewAcitvityWithView("More",More.class);
        	isFirst=0;
        	setCurrentTab(3);
        	isFirst=1;
        }else if(value.equals("login")){
        	openNewAcitvityWithView("Login",Login.class);
        }else if(value.equals("aboutus")){
        	openNewAcitvityWithView("aboutus",aboutus.class);
        }else if(value.equals("bookmark")){
        	openNewAcitvityWithView("bookmark",bookmark.class);
        	isFirst=0;
        	setCurrentTab(2);
        	isFirst=1;
        }else if(value.equals("register")){
        	openNewAcitvityWithView("register",register.class);
        }else if(value.equals("search")){
        	isFirst=0;
        	setCurrentTab(1);
        	isFirst=1;
        	openNewAcitvityWithView("search",search.class);
        }else if(value.equals("SearchSite")){
        	openNewAcitvityWithView("SearchSite",SearchSite.class);
        }else if(value.equals("TopHome")){
        	isFirst=0;
        	setCurrentTab(0);
        	isFirst=1;
        	openNewAcitvityWithView("TopHome",TopHome.class);
        }
	}
	
//    private enum VIEW_NAME {  
////    	VIEW_NAME("list"),
//        USERNAME("username"),
//        PASSWORD("password"),
//        USERID("userid"),
//        IS_REMEMBER_LOGIN("rememberLogin");
//        
//
//        private final String key;     
//		private CONSTANT_KEY(String key) {  
//		     this.key = key;  
//		}
//    }; 
	
	private void openNewAcitvityWithView(String title,Class<?> intentClass){
    	Intent newWinIntent =new Intent(this,intentClass);
		contentViewLayout.removeAllViews();
		View view = activityManager.startActivity(title, newWinIntent).getDecorView();
		contentViewLayout.addView(view, contentViewLayoutParams);
	}
	
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater info = getMenuInflater();
		info.inflate(R.menu.bottommenu, menu);
		
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
		if(item.getTitle().equals("Register"))
		{	
			openNewAcitvityWithView("Register",register.class);
		}
		else if(item.getTitle().equals("Login"))
		{
			openNewAcitvityWithView("Login",Login.class);
		}
		else if(item.getTitle().equals("Griffin"))
		{	
			openNewAcitvityWithView("Griffin",aboutus.class);
		}		
		return true;
	}

	public class SliderBarActivityDelegateImpl extends SliderBarActivityDelegate
	{
		/*
		 * Optional callback method
		 * called when users tap on the tab bar button
		 */
		protected void onTabChanged(int tabIndex) 
		{
			if(tabIndex==3)
			{

			}
			else
			{
				Animation alphaanimation = AnimationUtils.loadAnimation(TabBarFramework.this.bottomRadioGroup.getContext(), R.anim.tabbutton_alpha);
				Animation scaleanimation = AnimationUtils.loadAnimation(TabBarFramework.this.bottomRadioGroup.getContext(), R.anim.tabbutton_scale);
				Animation rotateanimation = AnimationUtils.loadAnimation(TabBarFramework.this.bottomRadioGroup.getContext(), R.anim.sliding);
				RadioGroup bottomRadioGroup = (RadioGroup)findViewById(R.id.bottomMenu);
				View v=bottomRadioGroup.getChildAt(tabIndex);
				v.startAnimation(scaleanimation);
				v.startAnimation(alphaanimation);

				

				//    		Button btnCover = (Button)findViewById(R.id.btncover);
				//    		btnCover.startAnimation(animation);
				//    		Log.d("onTabChanged",""+tabIndex);
			}
		}
	}
	
	private void initDatabase()
	{

		try
		{
			// get database file's absolute path
			String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;

			//check file
			File dir = new File(databaseFilename);
			//            if(dir.exists())
			//                return;

			// check or create database path
			dir = new File(DATABASE_PATH);

			if (!dir.exists())
				dir.mkdir();

			InputStream input = null;
			OutputStream output = null;
			// read database file flow from resource
			input = getResources().openRawResource(R.raw.web);

			try {
				output = new FileOutputStream(databaseFilename);

				// copy to output stream
				byte[] buffer = new byte[2048];
				int length;
				while ((length = input.read(buffer)) > 0) {
					output.write(buffer, 0, length);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				// close output stream
				try {
					output.flush();
					output.close();
				} catch (IOException e) {
				}
				// close input stream
				try {
					input.close();
				} catch (IOException e) {
				}
			}    
		}
		catch (Exception e)
		{

		}
	}


	@Override
	public void onPause() {
		super.onStop();
		
//		userlogic.loginOutWhenExit();
//		Log.d(TAG,"[onPause...]" );
	}
	@Override
	public void onSaveInstanceState (Bundle outState) {
		super.onSaveInstanceState(outState);
		
//		userlogic.loginOutWhenExit();
//		Log.d(TAG,"[onSaveInstanceState...]" );
	}
	@Override
	public void onStop() {
		super.onStop();
		
//		userlogic.loginOutWhenExit();
//		Log.d(TAG,"[onStop...]" );
	}

	

}